import Vue from 'vue'
import Vuex from 'vuex'
import APIClient from '../network';

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    Classify: [], // 商品分类
    OneList: [],  // 一级分类
    TwoList: [],  // 二 级分类
    TwoListItem: [],//  点击一级切换二级分类
    ListItem:[],  // 商品列表
  },
  mutations: {
    CLASSIFY(state) {
      APIClient.classifyList({}).then((res) => {
        // console.log(res.data.data)
        state.Classify = res.data.data
        state.OneList = []
        state.TwoList = []
        state.TwoListItem = []
        state.Classify.forEach((element) => {
          if (element.pid === 0) {
            state.OneList.push(element)
          }
        })
        state.Classify.forEach((element) => {
          if (element.pid !== 0) {
            state.TwoList.push(element)
          }
        })
        state.Classify.forEach((element) => {
          if (element.pid !== 0) {
            state.TwoListItem.push(element)
          }
        })
        // console.log(state.TwoList)
      })
        .catch((res) => {
          // console.log(res);
        });
    },
    CLICKONE(state, item) {
      // console.log(item.type)
      state.TwoListItem = []
      state.TwoList.forEach((element) => {
        if (element.type === item.type + '2') {
          state.TwoListItem.push(element)
        }
      })
    },
    CLICKONEALL(state) {
      state.TwoListItem = state.TwoList
      // console.log(state.TwoListItem)
    },
    LISTITEM(state){
      APIClient.ListItem({params:{ page:1,pageSize:20 }}).then((res) => {
        // console.log(res.data.data)
        state.ListItem = res.data.data
      })
        .catch((res) => {
          // console.log(res);
        });
    },
  },
  actions: {
    CLASSIFY(context) {
      context.commit('CLASSIFY')
    },
    CLICKONE(context, item) {
      context.commit('CLICKONE', item)
    },
    CLICKONEALL(context) {
      context.commit('CLICKONEALL')
    },
    LISTITEM(context) {
      context.commit('LISTITEM')
    }
  },
  modules: {
  }
})
